"""Inital migration

Revision ID: 37db3151f4ce
Revises: 
Create Date: 2020-12-28 00:42:59.675621

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy.engine.reflection import Inspector

# revision identifiers, used by Alembic.
revision = "37db3151f4ce"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)
    tables = inspector.get_table_names()
    # ### commands auto generated by Alembic - please adjust! ###

    if "steam_id_64" not in tables:
        op.create_table(
            "steam_id_64",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("steam_id_64", sa.String(), nullable=False),
            sa.Column("created", sa.DateTime(), nullable=True),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index(
            op.f("ix_steam_id_64_steam_id_64"),
            "steam_id_64",
            ["steam_id_64"],
            unique=True,
        )
    if "user_config" not in tables:
        op.create_table(
            "user_config",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("key", sa.String(), nullable=True),
            sa.Column("value", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index(op.f("ix_user_config_key"), "user_config", ["key"], unique=True)

    if "log_lines" not in tables:
        op.create_table(
            "log_lines",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("version", sa.Integer(), nullable=True),
            sa.Column("creation_time", sa.TIMESTAMP(), nullable=True),
            sa.Column("event_time", sa.DateTime(), nullable=False),
            sa.Column("type", sa.String(), nullable=True),
            sa.Column("player1_name", sa.String(), nullable=True),
            sa.Column("player1_steamid", sa.Integer(), nullable=True),
            sa.Column("player2_name", sa.String(), nullable=True),
            sa.Column("player2_steamid", sa.Integer(), nullable=True),
            sa.Column("raw", sa.String(), nullable=False),
            sa.Column("content", sa.String(), nullable=True),
            sa.ForeignKeyConstraint(
                ["player1_steamid"],
                ["steam_id_64.id"],
            ),
            sa.ForeignKeyConstraint(
                ["player2_steamid"],
                ["steam_id_64.id"],
            ),
            sa.PrimaryKeyConstraint("id"),
            sa.UniqueConstraint("event_time", "raw", name="unique_log_line"),
        )
        op.create_index(
            op.f("ix_log_lines_event_time"), "log_lines", ["event_time"], unique=False
        )
        op.create_index(
            op.f("ix_log_lines_player1_steamid"),
            "log_lines",
            ["player1_steamid"],
            unique=False,
        )
        op.create_index(
            op.f("ix_log_lines_player2_steamid"),
            "log_lines",
            ["player2_steamid"],
            unique=False,
        )
    if "player_blacklist" not in tables:
        op.create_table(
            "player_blacklist",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("playersteamid_id", sa.Integer(), nullable=False),
            sa.Column("is_blacklisted", sa.Boolean(), nullable=True),
            sa.Column("reason", sa.String(), nullable=True),
            sa.ForeignKeyConstraint(
                ["playersteamid_id"],
                ["steam_id_64.id"],
            ),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index(
            op.f("ix_player_blacklist_playersteamid_id"),
            "player_blacklist",
            ["playersteamid_id"],
            unique=True,
        )
    if "player_flags" not in tables:
        op.create_table(
            "player_flags",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("playersteamid_id", sa.Integer(), nullable=False),
            sa.Column("flag", sa.String(), nullable=False),
            sa.Column("comment", sa.String(), nullable=True),
            sa.Column("modified", sa.DateTime(), nullable=True),
            sa.ForeignKeyConstraint(
                ["playersteamid_id"],
                ["steam_id_64.id"],
            ),
            sa.PrimaryKeyConstraint("id"),
            sa.UniqueConstraint("playersteamid_id", "flag", name="unique_flag_steamid"),
        )
        op.create_index(
            op.f("ix_player_flags_flag"), "player_flags", ["flag"], unique=False
        )
        op.create_index(
            op.f("ix_player_flags_playersteamid_id"),
            "player_flags",
            ["playersteamid_id"],
            unique=False,
        )
    if "player_names" not in tables:
        op.create_table(
            "player_names",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("playersteamid_id", sa.Integer(), nullable=False),
            sa.Column("name", sa.String(), nullable=False),
            sa.Column("created", sa.DateTime(), nullable=True),
            sa.ForeignKeyConstraint(
                ["playersteamid_id"],
                ["steam_id_64.id"],
            ),
            sa.PrimaryKeyConstraint("id"),
            sa.UniqueConstraint("playersteamid_id", "name", name="unique_name_steamid"),
        )
        op.create_index(
            op.f("ix_player_names_playersteamid_id"),
            "player_names",
            ["playersteamid_id"],
            unique=False,
        )
    if "player_sessions" not in tables:
        op.create_table(
            "player_sessions",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("playersteamid_id", sa.Integer(), nullable=False),
            sa.Column("start", sa.DateTime(), nullable=True),
            sa.Column("end", sa.DateTime(), nullable=True),
            sa.Column("created", sa.DateTime(), nullable=True),
            sa.ForeignKeyConstraint(
                ["playersteamid_id"],
                ["steam_id_64.id"],
            ),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index(
            op.f("ix_player_sessions_playersteamid_id"),
            "player_sessions",
            ["playersteamid_id"],
            unique=False,
        )
    if "players_actions" not in tables:
        op.create_table(
            "players_actions",
            sa.Column("id", sa.Integer(), nullable=False),
            sa.Column("action_type", sa.String(), nullable=False),
            sa.Column("playersteamid_id", sa.Integer(), nullable=False),
            sa.Column("reason", sa.String(), nullable=True),
            sa.Column("by", sa.String(), nullable=True),
            sa.Column("time", sa.DateTime(), nullable=True),
            sa.ForeignKeyConstraint(
                ["playersteamid_id"],
                ["steam_id_64.id"],
            ),
            sa.PrimaryKeyConstraint("id"),
        )
        op.create_index(
            op.f("ix_players_actions_playersteamid_id"),
            "players_actions",
            ["playersteamid_id"],
            unique=False,
        )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_players_actions_playersteamid_id"), table_name="players_actions"
    )
    op.drop_table("players_actions")
    op.drop_index(
        op.f("ix_player_sessions_playersteamid_id"), table_name="player_sessions"
    )
    op.drop_table("player_sessions")
    op.drop_index(op.f("ix_player_names_playersteamid_id"), table_name="player_names")
    op.drop_table("player_names")
    op.drop_index(op.f("ix_player_flags_playersteamid_id"), table_name="player_flags")
    op.drop_index(op.f("ix_player_flags_flag"), table_name="player_flags")
    op.drop_table("player_flags")
    op.drop_index(
        op.f("ix_player_blacklist_playersteamid_id"), table_name="player_blacklist"
    )
    op.drop_table("player_blacklist")
    op.drop_index(op.f("ix_log_lines_player2_steamid"), table_name="log_lines")
    op.drop_index(op.f("ix_log_lines_player1_steamid"), table_name="log_lines")
    op.drop_index(op.f("ix_log_lines_event_time"), table_name="log_lines")
    op.drop_table("log_lines")
    op.drop_index(op.f("ix_user_config_key"), table_name="user_config")
    op.drop_table("user_config")
    op.drop_index(op.f("ix_steam_id_64_steam_id_64"), table_name="steam_id_64")
    op.drop_table("steam_id_64")
    # ### end Alembic commands ###
